/*********************************************************************************
 *      Copyright:  (C) 2022 xiezongyi<1764417007@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  mosquitto_conf.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(03/05/22)
 *         Author:  xiezongyi <1764417007@qq.com>
 *      ChangeLog:  1, Release initial version on "03/05/22 20:51:06"
 *                 
 ********************************************************************************/

#include <stdio.h>
#include <errno.h>
#include <string.h>

#include "iniparser.h"
#include "dictionary.h"
#include "mosquitto_conf.h"

#define PATH 	"/home/Xiezongyi/mqtt/ini/conf.ini"

int get_mqtt_conf(mqtt_ctx_t *mqtt) 
{
	dictionary			*ini = NULL;
	const char      	*clientid;
	const char      	*hostname;
	const char      	*pubtopic;
	const char      	*subtopic;
	const char      	*username;
	const char      	*passwd;
	const char			*platform;
	const char			*buf_json;
	const char			*buff;
	int         		port;
	int					limit_time = 0;
	sqlite3				*db = NULL;

	if(!mqtt)
	{
		printf("Invaild input argument in %s\n", __FUNCTION__);
		return -1;
	}

	//加载配置文件，将数据存储在dictionary结构中
	ini = iniparser_load(PATH);
	if(NULL == ini)
	{
		printf("iniparser_load failure: %s\n", strerror(errno));
		return -2;
	}

	if( strcmp(mqtt->platform, "aliyun") == 0)
	{	
		//获取字符串，若未找到则返回第三个参数的内容
		hostname = iniparser_getstring(ini, "aliyun_info:hostname", "DEF_BROKER_HOSTNAME");	
		strncpy(mqtt->hostname, hostname, sizeof(mqtt->hostname));

		username = iniparser_getstring(ini, "aliyun_info:username", "DEF_BROKER_USERNAME");
		strncpy(mqtt->username, username, sizeof(mqtt->username));

		passwd = iniparser_getstring(ini, "aliyun_info:passwd", "DEF_BROKER_PASSWD");
		strncpy(mqtt->passwd, passwd, sizeof(mqtt->passwd));

		clientid = iniparser_getstring(ini, "aliyun_info:id", "DEF_BROKER_CLIENTID");
		strncpy(mqtt->clientid, clientid, sizeof(mqtt->clientid));

		pubtopic = iniparser_getstring(ini, "aliyun_info:pubtopic", "DEF_BROKER_PUBTOPIC");
		strncpy(mqtt->pubtopic, pubtopic, sizeof(mqtt->pubtopic));

		subtopic = iniparser_getstring(ini, "aliyun_info:subtopic", "DEF_BROKER_PUBTOPIC");
		strncpy(mqtt->subtopic, subtopic, sizeof(mqtt->subtopic));

		//获取int类型的数
		port = iniparser_getint(ini, "aliyun_info:port", 0);	
		mqtt->port = port;

	}
	else if(strcmp(mqtt->platform, "local") == 0)
	{
		//获取字符串，若未找到则返回第三个参数的内容
        hostname = iniparser_getstring(ini, "local:hostname", "DEF_BROKER_HOSTNAME");
        strncpy(mqtt->hostname, hostname, sizeof(mqtt->hostname));

        username = iniparser_getstring(ini, "local:username", "DEF_BROKER_USERNAME");
        strncpy(mqtt->username, username, sizeof(mqtt->username));

        passwd = iniparser_getstring(ini, "local:passwd", "DEF_BROKER_PASSWD");
        strncpy(mqtt->passwd, passwd, sizeof(mqtt->passwd));

        clientid = iniparser_getstring(ini, "local:id", "DEF_BROKER_CLIENTID");
        strncpy(mqtt->clientid, clientid, sizeof(mqtt->clientid));

        pubtopic = iniparser_getstring(ini, "local:pubtopic", "DEF_BROKER_PUBTOPIC");
        strncpy(mqtt->pubtopic, pubtopic, sizeof(mqtt->pubtopic));

        subtopic = iniparser_getstring(ini, "local:subtopic", "DEF_BROKER_PUBTOPIC");
        strncpy(mqtt->subtopic, subtopic, sizeof(mqtt->subtopic));

        //获取int类型的数
        port = iniparser_getint(ini, "local:port", 0);
        mqtt->port = port;

	}
	else
	{
		return -3;
	}

	//释放dictionary对象（内存）
	iniparser_freedict(ini);

	return 0;
}

